﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChungNhanSinhVien.Infrastructures;
using ChungNhanSinhVien.Entities;

namespace ChungNhanSinhVien.Models
{
    public class AdminAccountRepository
    {
        Web_ChungNhanSinhVienEntities data;
        public AdminAccountRepository()
        {
            //if (data == null)
                data = new Web_ChungNhanSinhVienEntities();
        }

        public bool KiemTraTaiKhoan(string username, string matkhau, out AdminAccount kq)
        {
            string md5mk = new SharedFunc().CreateMD5Hash(matkhau);
            kq = (from m in data.AdminAccounts
                  where m.TenDangNhap.Equals(username)
                  && m.MatKhau.Equals(md5mk)
                  select m).FirstOrDefault();
            if (kq != null)
                return true;
            return false;
        }

        public bool LuuTaiKhoan(AdminAccount ad)
        {
            string md5mk = new SharedFunc().CreateMD5Hash(ad.MatKhau);
            var kq = (from m in data.AdminAccounts
                      where m.TenDangNhap.Equals(ad.TenDangNhap)
                      && m.MatKhau.Equals(md5mk)
                      select m).FirstOrDefault();
            if (kq != null)
            {
                ad.mkmoi = new SharedFunc().CreateMD5Hash(ad.mkmoi);
                kq.MatKhau = ad.mkmoi;
                data.SaveChanges();
                return true;
            }
            return false;
        }
    }

}